# 3.15 WS2818 RGB LED ## 3.15.1 Overview WS2812 RGB LED is an external control LED integrating control circuit and light emitting circuit. It adopts single-line return-to-zero code communication, and supports 256 gray levels to display full-colors. The integrated chip inside each pixels efficiently stabilizes color output. So it is widely used in lighting, display and decoration. ## 3.15.2 Schematic Diagram ![6-15-2](./media/6-15-2.png) From the Schematic Diagram, ws2812 connects and transmits data over a single wire, which is the communication method named single-bus return-to-zero code (single NZR). The data enters in serial through the DIN port, and each pixel receives and processes 24 bits data (R, G, B color channels with 8 bits each). For detailed information of transmission mode, please refer the specification of ws2812. ## 3.15.3 Code Blocks 1. ![6-15](./media/j46.png) This block initialize the ws2812 module. You only need to set its numbers and pin. 2. ![j47](./media/j47.png) This module appoints one certain ws2812 pixel to light up. You only need to input the ws2812 pixel number (starting from 0) and set its color. 3. ![j48](./media/j48.png) This block appoints a certain range of ws2812 pixels to light up. You only need to input two ws2812 pixel number (a starting and a ending pixel) and set their colors. 4. ![j49](./media/j49.png) This block sets colors of the LED. Three parameters (R, G, B) should be set. 5. ![image-j50](./media/j50.png) This block sets the brightness of ws2812 pixels. Its range is 0 ~ 255. 6. ![image-j51](./media/j51.png) This block turns off all ws2812 pixels. 7. ![j52](./media/j52.png) This block refreshes the state of each ws2812 pixel. Before every part that controls pixels, this block should be added after them. Otherwise, they are invalid settings. ## 3.15.4 Test Code ### 3.15.4.1 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-15-rgbLed.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![](./media/RGB.png), find and put ![6-15](./media/j46.png) under![j1](./media/j1.png) 3. In ![](./media/RGB.png), put ![6-15](./media/j50.png) under![j1](./media/j46.png) and set brightness to 50 to avoid glaring. 4. In ![control](./media/control.png), put ![j2](./media/j2.png) under ![6-15](./media/j50.png) ![6-15](./media/6-15-4-1-1.png) 5. In ![](./media/RGB.png), put ![j47](./media/j47.png) into ![j2](./media/j2.png), and set pixel to 0 and color to red. 6. In ![](./media/RGB.png), add a ![j47](./media/j52.png) to refresh the above settings so that ws2812 will light up. **Complete Test Code** ![6-15](./media/6-15-4-1-2.png) ### 3.15.4.2 Test Result After uploading code, the first pixel of ws2812 lights up in red. ## 3.15.5 Extension Light up all LED circulating red, green, and blue. ### 3.15.5.1 Test Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-15-rgbLed2.sb3` **Manually build blocks:** 1. In ![events](./media/events.png), find ![j1](./media/j1.png) block. 2. In ![RGB](./media/RGB.png), drag and put ![6-15](./media/j46.png) under ![j1](./media/j1.png) 3. In ![RGB](./media/RGB.png), drag and put ![6-15](./media/j50.png) under ![j1](./media/j46.png), and set brightness to 50 to avoid glaring. 4. In ![control](./media/control.png), put ![j2](./media/j2.png) under ![6-15](./media/j50.png) ![6-15](./media/6-15-4-1-1.png) 5. In ![RGB](./media/RGB.png), put ![j47](./media/j48.png) in ![j2](./media/j2.png), and set to fill pixel from “0” to “11”, and modify the color to red. 6. In ![RGB](./media/RGB.png), add a ![j47](./media/j52.png) to refresh the above settings so that ws2812 will light up. 7. Add a delay of 1S. ![6-15](./media/6-15-5-1-1.png) 8. Duplicate ![6-15](./media/6-15-5-1-2.png) and set the color to green. 9. Duplicate again ![6-15](./media/6-15-5-1-3.png) and set the color to blue. **Complete Test Code** ![6-15-5-1](./media/6-15-5-1.png) ### 3.15.5.2 Test Result After uploading code, ws2812 repeatedly lights up in red, green and blue with 1s each.